
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta charset="utf-8" />
  
  <!-- Licensed under the Apache 2.0 License -->
  <link rel="stylesheet" type="text/css" href="../../../../_static/fonts/open-sans/stylesheet.css" />
  <!-- Licensed under the SIL Open Font License -->
  <link rel="stylesheet" type="text/css" href="../../../../_static/fonts/source-serif-pro/source-serif-pro.css" />
  <link rel="stylesheet" type="text/css" href="../../../../_static/css/bootstrap.min.css" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
    <title>jmetal.algorithm.multiobjective.smpso &#8212; jMetalPy 1.5.3 documentation</title>
    <link rel="stylesheet" href="../../../../_static/guzzle.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" type="text/css" href="../../../../_static/custom.css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../../_static/language_data.js"></script>
    <script crossorigin="anonymous" integrity="sha256-Ae2Vz/4ePdIu6ZyI/5ZGsYnb+m0JlOmKPjt6XZ9JJkA=" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js"></script>
    <script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["$", "$"], ["\\(", "\\)"]], "processEscapes": true, "ignoreClass": "document", "processClass": "math|output_area"}})</script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />
  
   

  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../../index.html">jMetalPy 1.5.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../../index.html" accesskey="U">Module code</a> &#187;</li> 
      </ul>
    </div>
    <div class="container-wrapper">

      <div id="mobile-toggle">
        <a href="#"><span class="glyphicon glyphicon-align-justify" aria-hidden="true"></span></a>
      </div>
  <div id="left-column">
    <div class="sphinxsidebar"><a href="
    ../../../../index.html" class="text-logo">
    <img src="_static/jmetalpy.png" class="img-fluid" alt="jMetalPy 1.5.3 documentation">
    <br>
</a>

<div class="sidebar-block">
  <div class="sidebar-wrapper">
    Python version of the jMetal framework
  </div>
</div>
<div class="sidebar-block">
  <div class="sidebar-wrapper">
    <h2>Table Of Contents</h2>
  </div>
  <div class="sidebar-toc">
    
    
      <ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../tutorials.html">Getting started</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../multiobjective.algorithms.html">Multi-objective algorithms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../singleobjective.algorithms.html">Single-objective algorithms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../operators.html">Operators</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../problems.html">Problems</a></li>
</ul>

    
  </div>
</div>
<div class="sidebar-block">
  <div class="sidebar-wrapper">
    <div id="main-search">
      <form class="form-inline" action="../../../../search.html" method="GET" role="form">
        <div class="input-group">
          <input name="q" type="text" class="form-control" placeholder="Search...">
        </div>
        <input type="hidden" name="check_keywords" value="yes" />
        <input type="hidden" name="area" value="default" />
      </form>
    </div>
  </div>
</div>
      
    </div>
  </div>
        <div id="right-column">
          
          <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
              <li class="breadcrumb-item"><a href="../../../../index.html">Docs</a></li>
              
              <li class="breadcrumb-item"><a href="../../../index.html">Module code</a></li>
              
              <li class="breadcrumb-item">jmetal.algorithm.multiobjective.smpso</li>
            </ol>
          </nav>
          
          <div class="document clearer body">
            
  <h1>Source code for jmetal.algorithm.multiobjective.smpso</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">random</span>
<span class="kn">import</span> <span class="nn">threading</span>
<span class="kn">from</span> <span class="nn">copy</span> <span class="kn">import</span> <span class="n">copy</span>
<span class="kn">from</span> <span class="nn">math</span> <span class="kn">import</span> <span class="n">sqrt</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">TypeVar</span><span class="p">,</span> <span class="n">List</span><span class="p">,</span> <span class="n">Optional</span>

<span class="kn">import</span> <span class="nn">numpy</span>

<span class="kn">from</span> <span class="nn">jmetal.config</span> <span class="kn">import</span> <span class="n">store</span>
<span class="kn">from</span> <span class="nn">jmetal.core.algorithm</span> <span class="kn">import</span> <span class="n">ParticleSwarmOptimization</span><span class="p">,</span> <span class="n">DynamicAlgorithm</span>
<span class="kn">from</span> <span class="nn">jmetal.core.operator</span> <span class="kn">import</span> <span class="n">Mutation</span>
<span class="kn">from</span> <span class="nn">jmetal.core.problem</span> <span class="kn">import</span> <span class="n">FloatProblem</span><span class="p">,</span> <span class="n">DynamicProblem</span>
<span class="kn">from</span> <span class="nn">jmetal.core.solution</span> <span class="kn">import</span> <span class="n">FloatSolution</span>
<span class="kn">from</span> <span class="nn">jmetal.util.archive</span> <span class="kn">import</span> <span class="n">BoundedArchive</span><span class="p">,</span> <span class="n">ArchiveWithReferencePoint</span>
<span class="kn">from</span> <span class="nn">jmetal.util.comparator</span> <span class="kn">import</span> <span class="n">DominanceComparator</span>
<span class="kn">from</span> <span class="nn">jmetal.util.evaluator</span> <span class="kn">import</span> <span class="n">Evaluator</span>
<span class="kn">from</span> <span class="nn">jmetal.util.generator</span> <span class="kn">import</span> <span class="n">Generator</span>
<span class="kn">from</span> <span class="nn">jmetal.util.termination_criterion</span> <span class="kn">import</span> <span class="n">TerminationCriterion</span>

<span class="n">R</span> <span class="o">=</span> <span class="n">TypeVar</span><span class="p">(</span><span class="s1">&#39;R&#39;</span><span class="p">)</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">.. module:: SMPSO</span>
<span class="sd">   :platform: Unix, Windows</span>
<span class="sd">   :synopsis: Implementation of SMPSO.</span>

<span class="sd">.. moduleauthor:: Antonio Benítez-Hidalgo &lt;antonio.b@uma.es&gt;</span>
<span class="sd">&quot;&quot;&quot;</span>


<div class="viewcode-block" id="SMPSO"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso.html#jmetal.algorithm.multiobjective.smpso.SMPSO">[docs]</a><span class="k">class</span> <span class="nc">SMPSO</span><span class="p">(</span><span class="n">ParticleSwarmOptimization</span><span class="p">):</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
                 <span class="n">problem</span><span class="p">:</span> <span class="n">FloatProblem</span><span class="p">,</span>
                 <span class="n">swarm_size</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
                 <span class="n">mutation</span><span class="p">:</span> <span class="n">Mutation</span><span class="p">,</span>
                 <span class="n">leaders</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">BoundedArchive</span><span class="p">],</span>
                 <span class="n">termination_criterion</span><span class="p">:</span> <span class="n">TerminationCriterion</span> <span class="o">=</span> <span class="n">store</span><span class="o">.</span><span class="n">default_termination_criteria</span><span class="p">,</span>
                 <span class="n">swarm_generator</span><span class="p">:</span> <span class="n">Generator</span> <span class="o">=</span> <span class="n">store</span><span class="o">.</span><span class="n">default_generator</span><span class="p">,</span>
                 <span class="n">swarm_evaluator</span><span class="p">:</span> <span class="n">Evaluator</span> <span class="o">=</span> <span class="n">store</span><span class="o">.</span><span class="n">default_evaluator</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; This class implements the SMPSO algorithm as described in</span>

<span class="sd">        * SMPSO: A new PSO-based metaheuristic for multi-objective optimization</span>
<span class="sd">        * MCDM 2009. DOI: `&lt;http://dx.doi.org/10.1109/MCDM.2009.4938830/&gt;`_.</span>

<span class="sd">        The implementation of SMPSO provided in jMetalPy follows the algorithm template described in the algorithm</span>
<span class="sd">        templates section of the documentation.</span>

<span class="sd">        :param problem: The problem to solve.</span>
<span class="sd">        :param swarm_size: Size of the swarm.</span>
<span class="sd">        :param max_evaluations: Maximum number of evaluations/iterations.</span>
<span class="sd">        :param mutation: Mutation operator (see :py:mod:`jmetal.operator.mutation`).</span>
<span class="sd">        :param leaders: Archive for leaders.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">SMPSO</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span>
            <span class="n">problem</span><span class="o">=</span><span class="n">problem</span><span class="p">,</span>
            <span class="n">swarm_size</span><span class="o">=</span><span class="n">swarm_size</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">swarm_generator</span> <span class="o">=</span> <span class="n">swarm_generator</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">swarm_evaluator</span> <span class="o">=</span> <span class="n">swarm_evaluator</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">termination_criterion</span> <span class="o">=</span> <span class="n">termination_criterion</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">observable</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">termination_criterion</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">mutation_operator</span> <span class="o">=</span> <span class="n">mutation</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">leaders</span> <span class="o">=</span> <span class="n">leaders</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">c1_min</span> <span class="o">=</span> <span class="mf">1.5</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">c1_max</span> <span class="o">=</span> <span class="mf">2.5</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">c2_min</span> <span class="o">=</span> <span class="mf">1.5</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">c2_max</span> <span class="o">=</span> <span class="mf">2.5</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">r1_min</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">r1_max</span> <span class="o">=</span> <span class="mf">1.0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">r2_min</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">r2_max</span> <span class="o">=</span> <span class="mf">1.0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">min_weight</span> <span class="o">=</span> <span class="mf">0.1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_weight</span> <span class="o">=</span> <span class="mf">0.1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">change_velocity1</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">change_velocity2</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">dominance_comparator</span> <span class="o">=</span> <span class="n">DominanceComparator</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">speed</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">swarm_size</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">problem</span><span class="o">.</span><span class="n">number_of_variables</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">float</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">delta_max</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">delta_min</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">empty</span><span class="p">(</span><span class="n">problem</span><span class="o">.</span><span class="n">number_of_variables</span><span class="p">),</span> \
                                         <span class="n">numpy</span><span class="o">.</span><span class="n">empty</span><span class="p">(</span><span class="n">problem</span><span class="o">.</span><span class="n">number_of_variables</span><span class="p">)</span>

<div class="viewcode-block" id="SMPSO.create_initial_solutions"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso.html#jmetal.algorithm.multiobjective.smpso.SMPSO.create_initial_solutions">[docs]</a>    <span class="k">def</span> <span class="nf">create_initial_solutions</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">FloatSolution</span><span class="p">]:</span>
        <span class="k">return</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">swarm_generator</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">problem</span><span class="p">)</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">swarm_size</span><span class="p">)]</span></div>

<div class="viewcode-block" id="SMPSO.evaluate"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso.html#jmetal.algorithm.multiobjective.smpso.SMPSO.evaluate">[docs]</a>    <span class="k">def</span> <span class="nf">evaluate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">solution_list</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">FloatSolution</span><span class="p">]):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">swarm_evaluator</span><span class="o">.</span><span class="n">evaluate</span><span class="p">(</span><span class="n">solution_list</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">problem</span><span class="p">)</span></div>

<div class="viewcode-block" id="SMPSO.stopping_condition_is_met"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso.html#jmetal.algorithm.multiobjective.smpso.SMPSO.stopping_condition_is_met">[docs]</a>    <span class="k">def</span> <span class="nf">stopping_condition_is_met</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">bool</span><span class="p">:</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">termination_criterion</span><span class="o">.</span><span class="n">is_met</span></div>

<div class="viewcode-block" id="SMPSO.initialize_global_best"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso.html#jmetal.algorithm.multiobjective.smpso.SMPSO.initialize_global_best">[docs]</a>    <span class="k">def</span> <span class="nf">initialize_global_best</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">swarm</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">FloatSolution</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">particle</span> <span class="ow">in</span> <span class="n">swarm</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">leaders</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">copy</span><span class="p">(</span><span class="n">particle</span><span class="p">))</span></div>

<div class="viewcode-block" id="SMPSO.initialize_particle_best"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso.html#jmetal.algorithm.multiobjective.smpso.SMPSO.initialize_particle_best">[docs]</a>    <span class="k">def</span> <span class="nf">initialize_particle_best</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">swarm</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">FloatSolution</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">particle</span> <span class="ow">in</span> <span class="n">swarm</span><span class="p">:</span>
            <span class="n">particle</span><span class="o">.</span><span class="n">attributes</span><span class="p">[</span><span class="s1">&#39;local_best&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">copy</span><span class="p">(</span><span class="n">particle</span><span class="p">)</span></div>

<div class="viewcode-block" id="SMPSO.initialize_velocity"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso.html#jmetal.algorithm.multiobjective.smpso.SMPSO.initialize_velocity">[docs]</a>    <span class="k">def</span> <span class="nf">initialize_velocity</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">swarm</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">FloatSolution</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">problem</span><span class="o">.</span><span class="n">number_of_variables</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">delta_max</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">problem</span><span class="o">.</span><span class="n">upper_bound</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">problem</span><span class="o">.</span><span class="n">lower_bound</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="o">/</span> <span class="mf">2.0</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">delta_min</span> <span class="o">=</span> <span class="o">-</span><span class="mf">1.0</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">delta_max</span></div>

<div class="viewcode-block" id="SMPSO.update_velocity"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso.html#jmetal.algorithm.multiobjective.smpso.SMPSO.update_velocity">[docs]</a>    <span class="k">def</span> <span class="nf">update_velocity</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">swarm</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">FloatSolution</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">swarm_size</span><span class="p">):</span>
            <span class="n">best_particle</span> <span class="o">=</span> <span class="n">copy</span><span class="p">(</span><span class="n">swarm</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">attributes</span><span class="p">[</span><span class="s1">&#39;local_best&#39;</span><span class="p">])</span>
            <span class="n">best_global</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">select_global_best</span><span class="p">()</span>

            <span class="n">r1</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">r1_min</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">r1_max</span><span class="p">),</span> <span class="mi">1</span><span class="p">)</span>
            <span class="n">r2</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">r2_min</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">r2_max</span><span class="p">),</span> <span class="mi">1</span><span class="p">)</span>
            <span class="n">c1</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">c1_min</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">c1_max</span><span class="p">),</span> <span class="mi">1</span><span class="p">)</span>
            <span class="n">c2</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">c2_min</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">c2_max</span><span class="p">),</span> <span class="mi">1</span><span class="p">)</span>
            <span class="n">wmax</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_weight</span>
            <span class="n">wmin</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">min_weight</span>

            <span class="k">for</span> <span class="n">var</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">swarm</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">number_of_variables</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">speed</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">var</span><span class="p">]</span> <span class="o">=</span> \
                    <span class="bp">self</span><span class="o">.</span><span class="n">__velocity_constriction</span><span class="p">(</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">__constriction_coefficient</span><span class="p">(</span><span class="n">c1</span><span class="p">,</span> <span class="n">c2</span><span class="p">)</span> <span class="o">*</span>
                        <span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">__inertia_weight</span><span class="p">(</span><span class="n">wmax</span><span class="p">)</span>
                          <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">speed</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">var</span><span class="p">])</span>
                         <span class="o">+</span> <span class="p">(</span><span class="n">c1</span> <span class="o">*</span> <span class="n">r1</span> <span class="o">*</span> <span class="p">(</span><span class="n">best_particle</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">var</span><span class="p">]</span> <span class="o">-</span> <span class="n">swarm</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">var</span><span class="p">]))</span>
                         <span class="o">+</span> <span class="p">(</span><span class="n">c2</span> <span class="o">*</span> <span class="n">r2</span> <span class="o">*</span> <span class="p">(</span><span class="n">best_global</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">var</span><span class="p">]</span> <span class="o">-</span> <span class="n">swarm</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">var</span><span class="p">]))</span>
                         <span class="p">),</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">delta_max</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">delta_min</span><span class="p">,</span> <span class="n">var</span><span class="p">)</span></div>

<div class="viewcode-block" id="SMPSO.update_position"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso.html#jmetal.algorithm.multiobjective.smpso.SMPSO.update_position">[docs]</a>    <span class="k">def</span> <span class="nf">update_position</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">swarm</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">FloatSolution</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">swarm_size</span><span class="p">):</span>
            <span class="n">particle</span> <span class="o">=</span> <span class="n">swarm</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>

            <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">particle</span><span class="o">.</span><span class="n">number_of_variables</span><span class="p">):</span>
                <span class="n">particle</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">speed</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span>

                <span class="k">if</span> <span class="n">particle</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">problem</span><span class="o">.</span><span class="n">lower_bound</span><span class="p">[</span><span class="n">j</span><span class="p">]:</span>
                    <span class="n">particle</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">problem</span><span class="o">.</span><span class="n">lower_bound</span><span class="p">[</span><span class="n">j</span><span class="p">]</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">speed</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">*=</span> <span class="bp">self</span><span class="o">.</span><span class="n">change_velocity1</span>

                <span class="k">if</span> <span class="n">particle</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">problem</span><span class="o">.</span><span class="n">upper_bound</span><span class="p">[</span><span class="n">j</span><span class="p">]:</span>
                    <span class="n">particle</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">problem</span><span class="o">.</span><span class="n">upper_bound</span><span class="p">[</span><span class="n">j</span><span class="p">]</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">speed</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">*=</span> <span class="bp">self</span><span class="o">.</span><span class="n">change_velocity2</span></div>

<div class="viewcode-block" id="SMPSO.update_global_best"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso.html#jmetal.algorithm.multiobjective.smpso.SMPSO.update_global_best">[docs]</a>    <span class="k">def</span> <span class="nf">update_global_best</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">swarm</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">FloatSolution</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">particle</span> <span class="ow">in</span> <span class="n">swarm</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">leaders</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">copy</span><span class="p">(</span><span class="n">particle</span><span class="p">))</span></div>

<div class="viewcode-block" id="SMPSO.update_particle_best"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso.html#jmetal.algorithm.multiobjective.smpso.SMPSO.update_particle_best">[docs]</a>    <span class="k">def</span> <span class="nf">update_particle_best</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">swarm</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">FloatSolution</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">swarm_size</span><span class="p">):</span>
            <span class="n">flag</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">dominance_comparator</span><span class="o">.</span><span class="n">compare</span><span class="p">(</span>
                <span class="n">swarm</span><span class="p">[</span><span class="n">i</span><span class="p">],</span>
                <span class="n">swarm</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">attributes</span><span class="p">[</span><span class="s1">&#39;local_best&#39;</span><span class="p">])</span>
            <span class="k">if</span> <span class="n">flag</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">swarm</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">attributes</span><span class="p">[</span><span class="s1">&#39;local_best&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">copy</span><span class="p">(</span><span class="n">swarm</span><span class="p">[</span><span class="n">i</span><span class="p">])</span></div>

<div class="viewcode-block" id="SMPSO.perturbation"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso.html#jmetal.algorithm.multiobjective.smpso.SMPSO.perturbation">[docs]</a>    <span class="k">def</span> <span class="nf">perturbation</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">swarm</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">FloatSolution</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">swarm_size</span><span class="p">):</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">i</span> <span class="o">%</span> <span class="mi">6</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">mutation_operator</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">swarm</span><span class="p">[</span><span class="n">i</span><span class="p">])</span></div>

<div class="viewcode-block" id="SMPSO.select_global_best"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso.html#jmetal.algorithm.multiobjective.smpso.SMPSO.select_global_best">[docs]</a>    <span class="k">def</span> <span class="nf">select_global_best</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">FloatSolution</span><span class="p">:</span>
        <span class="n">leaders</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">leaders</span><span class="o">.</span><span class="n">solution_list</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">leaders</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">2</span><span class="p">:</span>
            <span class="n">particles</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">leaders</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">leaders</span><span class="o">.</span><span class="n">comparator</span><span class="o">.</span><span class="n">compare</span><span class="p">(</span><span class="n">particles</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">particles</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">&lt;</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">best_global</span> <span class="o">=</span> <span class="n">copy</span><span class="p">(</span><span class="n">particles</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">best_global</span> <span class="o">=</span> <span class="n">copy</span><span class="p">(</span><span class="n">particles</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">best_global</span> <span class="o">=</span> <span class="n">copy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">leaders</span><span class="o">.</span><span class="n">solution_list</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>

        <span class="k">return</span> <span class="n">best_global</span></div>

    <span class="k">def</span> <span class="nf">__velocity_constriction</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">delta_max</span><span class="p">:</span> <span class="p">[],</span> <span class="n">delta_min</span><span class="p">:</span> <span class="p">[],</span> <span class="n">variable_index</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>
        <span class="n">result</span> <span class="o">=</span> <span class="n">value</span>
        <span class="k">if</span> <span class="n">value</span> <span class="o">&gt;</span> <span class="n">delta_max</span><span class="p">[</span><span class="n">variable_index</span><span class="p">]:</span>
            <span class="n">result</span> <span class="o">=</span> <span class="n">delta_max</span><span class="p">[</span><span class="n">variable_index</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">value</span> <span class="o">&lt;</span> <span class="n">delta_min</span><span class="p">[</span><span class="n">variable_index</span><span class="p">]:</span>
            <span class="n">result</span> <span class="o">=</span> <span class="n">delta_min</span><span class="p">[</span><span class="n">variable_index</span><span class="p">]</span>

        <span class="k">return</span> <span class="n">result</span>

    <span class="k">def</span> <span class="nf">__inertia_weight</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">wmax</span><span class="p">:</span> <span class="nb">float</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">wmax</span>

    <span class="k">def</span> <span class="nf">__constriction_coefficient</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">c1</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">c2</span><span class="p">:</span> <span class="nb">float</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>
        <span class="n">rho</span> <span class="o">=</span> <span class="n">c1</span> <span class="o">+</span> <span class="n">c2</span>
        <span class="k">if</span> <span class="n">rho</span> <span class="o">&lt;=</span> <span class="mi">4</span><span class="p">:</span>
            <span class="n">result</span> <span class="o">=</span> <span class="mf">1.0</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">result</span> <span class="o">=</span> <span class="mf">2.0</span> <span class="o">/</span> <span class="p">(</span><span class="mf">2.0</span> <span class="o">-</span> <span class="n">rho</span> <span class="o">-</span> <span class="n">sqrt</span><span class="p">(</span><span class="nb">pow</span><span class="p">(</span><span class="n">rho</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">)</span> <span class="o">-</span> <span class="mf">4.0</span> <span class="o">*</span> <span class="n">rho</span><span class="p">))</span>

        <span class="k">return</span> <span class="n">result</span>

<div class="viewcode-block" id="SMPSO.init_progress"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso.html#jmetal.algorithm.multiobjective.smpso.SMPSO.init_progress">[docs]</a>    <span class="k">def</span> <span class="nf">init_progress</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">evaluations</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">swarm_size</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">leaders</span><span class="o">.</span><span class="n">compute_density_estimator</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">initialize_velocity</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">solutions</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">initialize_particle_best</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">solutions</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">initialize_global_best</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">solutions</span><span class="p">)</span></div>

<div class="viewcode-block" id="SMPSO.update_progress"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso.html#jmetal.algorithm.multiobjective.smpso.SMPSO.update_progress">[docs]</a>    <span class="k">def</span> <span class="nf">update_progress</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">evaluations</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">swarm_size</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">leaders</span><span class="o">.</span><span class="n">compute_density_estimator</span><span class="p">()</span>

        <span class="n">observable_data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_observable_data</span><span class="p">()</span>
        <span class="n">observable_data</span><span class="p">[</span><span class="s1">&#39;SOLUTIONS&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">leaders</span><span class="o">.</span><span class="n">solution_list</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">observable</span><span class="o">.</span><span class="n">notify_all</span><span class="p">(</span><span class="o">**</span><span class="n">observable_data</span><span class="p">)</span></div>

<div class="viewcode-block" id="SMPSO.get_result"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso.html#jmetal.algorithm.multiobjective.smpso.SMPSO.get_result">[docs]</a>    <span class="k">def</span> <span class="nf">get_result</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">FloatSolution</span><span class="p">]:</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">leaders</span><span class="o">.</span><span class="n">solution_list</span></div>

<div class="viewcode-block" id="SMPSO.get_name"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso.html#jmetal.algorithm.multiobjective.smpso.SMPSO.get_name">[docs]</a>    <span class="k">def</span> <span class="nf">get_name</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
        <span class="k">return</span> <span class="s1">&#39;SMPSO&#39;</span></div></div>


<div class="viewcode-block" id="DynamicSMPSO"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso_dynamic.html#jmetal.algorithm.multiobjective.smpso.DynamicSMPSO">[docs]</a><span class="k">class</span> <span class="nc">DynamicSMPSO</span><span class="p">(</span><span class="n">SMPSO</span><span class="p">,</span> <span class="n">DynamicAlgorithm</span><span class="p">):</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
                 <span class="n">problem</span><span class="p">:</span> <span class="n">DynamicProblem</span><span class="p">[</span><span class="n">FloatSolution</span><span class="p">],</span>
                 <span class="n">swarm_size</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
                 <span class="n">mutation</span><span class="p">:</span> <span class="n">Mutation</span><span class="p">,</span>
                 <span class="n">leaders</span><span class="p">:</span> <span class="n">BoundedArchive</span><span class="p">,</span>
                 <span class="n">termination_criterion</span><span class="p">:</span> <span class="n">TerminationCriterion</span> <span class="o">=</span> <span class="n">store</span><span class="o">.</span><span class="n">default_termination_criteria</span><span class="p">,</span>
                 <span class="n">swarm_generator</span><span class="p">:</span> <span class="n">Generator</span> <span class="o">=</span> <span class="n">store</span><span class="o">.</span><span class="n">default_generator</span><span class="p">,</span>
                 <span class="n">swarm_evaluator</span><span class="p">:</span> <span class="n">Evaluator</span> <span class="o">=</span> <span class="n">store</span><span class="o">.</span><span class="n">default_evaluator</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">DynamicSMPSO</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span>
            <span class="n">problem</span><span class="o">=</span><span class="n">problem</span><span class="p">,</span>
            <span class="n">swarm_size</span><span class="o">=</span><span class="n">swarm_size</span><span class="p">,</span>
            <span class="n">mutation</span><span class="o">=</span><span class="n">mutation</span><span class="p">,</span>
            <span class="n">leaders</span><span class="o">=</span><span class="n">leaders</span><span class="p">,</span>
            <span class="n">termination_criterion</span><span class="o">=</span><span class="n">termination_criterion</span><span class="p">,</span>
            <span class="n">swarm_generator</span><span class="o">=</span><span class="n">swarm_generator</span><span class="p">,</span>
            <span class="n">swarm_evaluator</span><span class="o">=</span><span class="n">swarm_evaluator</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">completed_iterations</span> <span class="o">=</span> <span class="mi">0</span>

<div class="viewcode-block" id="DynamicSMPSO.restart"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso_dynamic.html#jmetal.algorithm.multiobjective.smpso.DynamicSMPSO.restart">[docs]</a>    <span class="k">def</span> <span class="nf">restart</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">solutions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">create_initial_solutions</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">solutions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">evaluate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">solutions</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">leaders</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">leaders</span><span class="o">.</span><span class="n">maximum_size</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">initialize_velocity</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">solutions</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">initialize_particle_best</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">solutions</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">initialize_global_best</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">solutions</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">init_progress</span><span class="p">()</span></div>

<div class="viewcode-block" id="DynamicSMPSO.update_progress"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso_dynamic.html#jmetal.algorithm.multiobjective.smpso.DynamicSMPSO.update_progress">[docs]</a>    <span class="k">def</span> <span class="nf">update_progress</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">problem</span><span class="o">.</span><span class="n">the_problem_has_changed</span><span class="p">():</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">restart</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">problem</span><span class="o">.</span><span class="n">clear_changed</span><span class="p">()</span>

        <span class="n">observable_data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_observable_data</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">observable</span><span class="o">.</span><span class="n">notify_all</span><span class="p">(</span><span class="o">**</span><span class="n">observable_data</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">evaluations</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">swarm_size</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">leaders</span><span class="o">.</span><span class="n">compute_density_estimator</span><span class="p">()</span></div>

<div class="viewcode-block" id="DynamicSMPSO.stopping_condition_is_met"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso_dynamic.html#jmetal.algorithm.multiobjective.smpso.DynamicSMPSO.stopping_condition_is_met">[docs]</a>    <span class="k">def</span> <span class="nf">stopping_condition_is_met</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">termination_criterion</span><span class="o">.</span><span class="n">is_met</span><span class="p">:</span>
            <span class="n">observable_data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_observable_data</span><span class="p">()</span>
            <span class="n">observable_data</span><span class="p">[</span><span class="s1">&#39;termination_criterion_is_met&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">observable</span><span class="o">.</span><span class="n">notify_all</span><span class="p">(</span><span class="o">**</span><span class="n">observable_data</span><span class="p">)</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">restart</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">init_progress</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">completed_iterations</span> <span class="o">+=</span> <span class="mi">1</span></div></div>


<div class="viewcode-block" id="SMPSORP"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso_preference.html#jmetal.algorithm.multiobjective.smpso.SMPSORP">[docs]</a><span class="k">class</span> <span class="nc">SMPSORP</span><span class="p">(</span><span class="n">SMPSO</span><span class="p">):</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
                 <span class="n">problem</span><span class="p">:</span> <span class="n">FloatProblem</span><span class="p">,</span>
                 <span class="n">swarm_size</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
                 <span class="n">mutation</span><span class="p">:</span> <span class="n">Mutation</span><span class="p">,</span>
                 <span class="n">reference_points</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="nb">float</span><span class="p">]],</span>
                 <span class="n">leaders</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">ArchiveWithReferencePoint</span><span class="p">],</span>
                 <span class="n">termination_criterion</span><span class="p">:</span> <span class="n">TerminationCriterion</span><span class="p">,</span>
                 <span class="n">swarm_generator</span><span class="p">:</span> <span class="n">Generator</span> <span class="o">=</span> <span class="n">store</span><span class="o">.</span><span class="n">default_generator</span><span class="p">,</span>
                 <span class="n">swarm_evaluator</span><span class="p">:</span> <span class="n">Evaluator</span> <span class="o">=</span> <span class="n">store</span><span class="o">.</span><span class="n">default_evaluator</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; This class implements the SMPSORP algorithm.</span>

<span class="sd">        :param problem: The problem to solve.</span>
<span class="sd">        :param swarm_size:</span>
<span class="sd">        :param mutation:</span>
<span class="sd">        :param leaders: List of bounded archives.</span>
<span class="sd">        :param swarm_evaluator: An evaluator object to evaluate the solutions in the population.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">SMPSORP</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span>
            <span class="n">problem</span><span class="o">=</span><span class="n">problem</span><span class="p">,</span>
            <span class="n">swarm_size</span><span class="o">=</span><span class="n">swarm_size</span><span class="p">,</span>
            <span class="n">mutation</span><span class="o">=</span><span class="n">mutation</span><span class="p">,</span>
            <span class="n">leaders</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
            <span class="n">swarm_generator</span><span class="o">=</span><span class="n">swarm_generator</span><span class="p">,</span>
            <span class="n">swarm_evaluator</span><span class="o">=</span><span class="n">swarm_evaluator</span><span class="p">,</span>
            <span class="n">termination_criterion</span><span class="o">=</span><span class="n">termination_criterion</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">leaders</span> <span class="o">=</span> <span class="n">leaders</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">reference_points</span> <span class="o">=</span> <span class="n">reference_points</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lock</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Lock</span><span class="p">()</span>

        <span class="n">thread</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">_change_reference_point</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="bp">self</span><span class="p">,))</span>
        <span class="n">thread</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>

<div class="viewcode-block" id="SMPSORP.initialize_global_best"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso_preference.html#jmetal.algorithm.multiobjective.smpso.SMPSORP.initialize_global_best">[docs]</a>    <span class="k">def</span> <span class="nf">initialize_global_best</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">swarm</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">FloatSolution</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">particle</span> <span class="ow">in</span> <span class="n">swarm</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">leader</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">leaders</span><span class="p">:</span>
                <span class="n">leader</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">copy</span><span class="p">(</span><span class="n">particle</span><span class="p">))</span></div>

<div class="viewcode-block" id="SMPSORP.update_global_best"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso_preference.html#jmetal.algorithm.multiobjective.smpso.SMPSORP.update_global_best">[docs]</a>    <span class="k">def</span> <span class="nf">update_global_best</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">swarm</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">FloatSolution</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">particle</span> <span class="ow">in</span> <span class="n">swarm</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">leader</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">leaders</span><span class="p">:</span>
                <span class="n">leader</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">copy</span><span class="p">(</span><span class="n">particle</span><span class="p">))</span></div>

<div class="viewcode-block" id="SMPSORP.select_global_best"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso_preference.html#jmetal.algorithm.multiobjective.smpso.SMPSORP.select_global_best">[docs]</a>    <span class="k">def</span> <span class="nf">select_global_best</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">FloatSolution</span><span class="p">:</span>
        <span class="n">selected</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="n">selected_swarm_index</span> <span class="o">=</span> <span class="mi">0</span>

        <span class="k">while</span> <span class="ow">not</span> <span class="n">selected</span><span class="p">:</span>
            <span class="n">selected_swarm_index</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">leaders</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">leaders</span><span class="p">[</span><span class="n">selected_swarm_index</span><span class="p">]</span><span class="o">.</span><span class="n">solution_list</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">selected</span> <span class="o">=</span> <span class="kc">True</span>

        <span class="n">leaders</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">leaders</span><span class="p">[</span><span class="n">selected_swarm_index</span><span class="p">]</span><span class="o">.</span><span class="n">solution_list</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">leaders</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">2</span><span class="p">:</span>
            <span class="n">particles</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">leaders</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">leaders</span><span class="p">[</span><span class="n">selected_swarm_index</span><span class="p">]</span><span class="o">.</span><span class="n">comparator</span><span class="o">.</span><span class="n">compare</span><span class="p">(</span><span class="n">particles</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">particles</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">&lt;</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">best_global</span> <span class="o">=</span> <span class="n">copy</span><span class="p">(</span><span class="n">particles</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">best_global</span> <span class="o">=</span> <span class="n">copy</span><span class="p">(</span><span class="n">particles</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">best_global</span> <span class="o">=</span> <span class="n">copy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">leaders</span><span class="p">[</span><span class="n">selected_swarm_index</span><span class="p">]</span><span class="o">.</span><span class="n">solution_list</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>

        <span class="k">return</span> <span class="n">best_global</span></div>

<div class="viewcode-block" id="SMPSORP.init_progress"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso_preference.html#jmetal.algorithm.multiobjective.smpso.SMPSORP.init_progress">[docs]</a>    <span class="k">def</span> <span class="nf">init_progress</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">evaluations</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">swarm_size</span>

        <span class="k">for</span> <span class="n">leader</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">leaders</span><span class="p">:</span>
            <span class="n">leader</span><span class="o">.</span><span class="n">compute_density_estimator</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">initialize_velocity</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">solutions</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">initialize_particle_best</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">solutions</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">initialize_global_best</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">solutions</span><span class="p">)</span></div>

<div class="viewcode-block" id="SMPSORP.update_progress"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso_preference.html#jmetal.algorithm.multiobjective.smpso.SMPSORP.update_progress">[docs]</a>    <span class="k">def</span> <span class="nf">update_progress</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">evaluations</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">swarm_size</span>

        <span class="k">for</span> <span class="n">leader</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">leaders</span><span class="p">:</span>
            <span class="n">leader</span><span class="o">.</span><span class="n">filter</span><span class="p">()</span>
            <span class="n">leader</span><span class="o">.</span><span class="n">compute_density_estimator</span><span class="p">()</span>

        <span class="n">observable_data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_observable_data</span><span class="p">()</span>
        <span class="n">observable_data</span><span class="p">[</span><span class="s1">&#39;REFERENCE_POINT&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_reference_point</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">observable</span><span class="o">.</span><span class="n">notify_all</span><span class="p">(</span><span class="o">**</span><span class="n">observable_data</span><span class="p">)</span></div>

<div class="viewcode-block" id="SMPSORP.update_reference_point"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso_preference.html#jmetal.algorithm.multiobjective.smpso.SMPSORP.update_reference_point">[docs]</a>    <span class="k">def</span> <span class="nf">update_reference_point</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">new_reference_points</span><span class="p">:</span> <span class="nb">list</span><span class="p">):</span>
        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">lock</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">reference_points</span> <span class="o">=</span> <span class="n">new_reference_points</span>

            <span class="k">for</span> <span class="n">index</span><span class="p">,</span> <span class="n">archive</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">leaders</span><span class="p">):</span>
                <span class="n">archive</span><span class="o">.</span><span class="n">update_reference_point</span><span class="p">(</span><span class="n">new_reference_points</span><span class="p">[</span><span class="n">index</span><span class="p">])</span></div>

<div class="viewcode-block" id="SMPSORP.get_reference_point"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso_preference.html#jmetal.algorithm.multiobjective.smpso.SMPSORP.get_reference_point">[docs]</a>    <span class="k">def</span> <span class="nf">get_reference_point</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">lock</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">reference_points</span></div>

<div class="viewcode-block" id="SMPSORP.get_result"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso_preference.html#jmetal.algorithm.multiobjective.smpso.SMPSORP.get_result">[docs]</a>    <span class="k">def</span> <span class="nf">get_result</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">FloatSolution</span><span class="p">]:</span>
        <span class="n">result</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="k">for</span> <span class="n">leader</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">leaders</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">solution</span> <span class="ow">in</span> <span class="n">leader</span><span class="o">.</span><span class="n">solution_list</span><span class="p">:</span>
                <span class="n">result</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">solution</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">result</span></div>

<div class="viewcode-block" id="SMPSORP.get_name"><a class="viewcode-back" href="../../../../api/algorithm/multiobjective/psos/smpso_preference.html#jmetal.algorithm.multiobjective.smpso.SMPSORP.get_name">[docs]</a>    <span class="k">def</span> <span class="nf">get_name</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
        <span class="k">return</span> <span class="s1">&#39;SMPSO/RP&#39;</span></div></div>


<span class="k">def</span> <span class="nf">_change_reference_point</span><span class="p">(</span><span class="n">algorithm</span><span class="p">:</span> <span class="n">SMPSORP</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Auxiliar function to read new reference points from the keyboard for the SMPSO/RP algorithm</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">number_of_reference_points</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">algorithm</span><span class="o">.</span><span class="n">reference_points</span><span class="p">)</span>
    <span class="n">number_of_objectives</span> <span class="o">=</span> <span class="n">algorithm</span><span class="o">.</span><span class="n">problem</span><span class="o">.</span><span class="n">number_of_objectives</span>

    <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
        <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;Enter </span><span class="si">{number_of_reference_points}</span><span class="s1">-points of dimension </span><span class="si">{number_of_objectives}</span><span class="s1">: &#39;</span><span class="p">)</span>
        <span class="n">read</span> <span class="o">=</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">input</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">()]</span>

        <span class="c1"># Update reference points</span>
        <span class="n">reference_points</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">read</span><span class="p">),</span> <span class="n">number_of_objectives</span><span class="p">):</span>
            <span class="n">reference_points</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">read</span><span class="p">[</span><span class="n">i</span><span class="p">:</span><span class="n">i</span> <span class="o">+</span> <span class="n">number_of_objectives</span><span class="p">])</span>

        <span class="n">algorithm</span><span class="o">.</span><span class="n">update_reference_point</span><span class="p">(</span><span class="n">reference_points</span><span class="p">)</span>
</pre></div>

          </div>
            
        </div>
        <div class="clearfix"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../../index.html">jMetalPy 1.5.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li> 
      </ul>
    </div>
<script type="text/javascript">
  $("#mobile-toggle a").click(function () {
    $("#left-column").toggle();
  });
</script>
<script type="text/javascript" src="../../../../_static/js/bootstrap.js"></script>
  <div class="footer">
    &copy; Copyright 2019, Antonio Benítez-Hidalgo. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>.
  </div>
  </body>
</html>